package com.bussiness.system.service;

import com.bussiness.system.dao.*;
import com.bussiness.system.model.MyDeptPostsEntity;
import com.bussiness.system.model.MyDeptUserEntity;
import com.bussiness.system.model.MyRoleEntity;
import com.bussiness.system.model.MyUserEntity;
import com.bussiness.system.vo.MyDeptPostsVo;
import com.bussiness.system.vo.MyDeptRoleVo;
import com.bussiness.system.vo.MyDeptUserVo;
import com.bussiness.system.dto.MyDeptRoleDto;
import com.bussiness.system.dto.MyDeptUserDto;
import com.common.util.TreeVoUtil;
import com.common.vo.TreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: zyj
 * @create: 2020-01-07 11:54
 **/
@Service
public class DeptPostsRoleService {

    @Autowired
    MyDeptPostsDao myDeptPostsDao;

    @Autowired
    MyDeptRoleDao myDeptRoleDao;

    @Autowired
    private TreeVoUtil treeVoUtil;

    @Autowired
    MyDeptUserDao myDeptUserDao;

    @Autowired
    private MyRoleDao myRoleDao;

    @Autowired
    private MyUserDao myUserDao;

    public TreeVo getTreeVo(String parentId) throws Exception{
        List<MyDeptPostsEntity> cloudDeptPostsEntitys = myDeptPostsDao.selectAll();
        List<TreeVo> listTree = coverTreeVoList(cloudDeptPostsEntitys);
        return treeVoUtil.getTreeVo(listTree, parentId);
    }

    /**
     * 根据部门id获取
     * @author zyj
     * @date 2019/9/24 20:07
     * @param deptId
     * @return
     */
    public List<MyDeptUserVo> getUserListByDeptId(Long deptId){
        List<MyDeptUserEntity> cloudDeptUserEntities = myDeptUserDao.findByDeptId(deptId);
        if(null==cloudDeptUserEntities||cloudDeptUserEntities.isEmpty()){
            return null;
        }

        List<MyDeptUserVo> list = new ArrayList<>();
        StringBuffer userIds = new StringBuffer();
        for(int i=0;i<cloudDeptUserEntities.size();i++){
            MyDeptUserEntity myDeptUserEntity = cloudDeptUserEntities.get(i);
            userIds.append(myDeptUserEntity.getUserId());
            if(i<cloudDeptUserEntities.size()-1){
                userIds.append(",");
            }
        }

        for(int j=0;j<cloudDeptUserEntities.size();j++){
            MyDeptUserEntity cloudDeptUserEntity = cloudDeptUserEntities.get(j);
            MyDeptUserVo vo = new MyDeptUserVo();
            vo.setId(String.valueOf(cloudDeptUserEntity.getId()));
            vo.setDeptId(String.valueOf(cloudDeptUserEntity.getDeptId()));
            vo.setStatus(cloudDeptUserEntity.getStatus());
            vo.setUserId(String.valueOf(cloudDeptUserEntity.getUserId()));

            list.add(vo);
        }

        return list;
    }

    /**
     * list转tree
     * @author zyj
     * @date 2019/9/24 11:14
     * @return
     */
    public List<TreeVo> getTreeVoList() throws Exception{
        List<MyDeptPostsEntity> cloudDeptPostsEntitys = myDeptPostsDao.selectAll();
        List<TreeVo> listTree = coverTreeVoList(cloudDeptPostsEntitys);
        return treeVoUtil.convertTreeVo(listTree,String.valueOf(0));
    }

    /**
     * 根据部门id获取部门角色列表
     * @author zyj
     * @date 2019/9/24 18:55
     * @param
     * @return
     */
    public List<MyDeptRoleVo> getRoleListByDeptId(Long deptId){
        List<Map<String, Object>> list = myDeptRoleDao.findRoleByDeptId(deptId);
        if(null==list||list.isEmpty()){
            return null;
        }
        List<MyDeptRoleVo> vos = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            Map<String, Object> myDeptRoleEntity = list.get(i);
            MyDeptRoleVo cloudDeptRoleVo = new MyDeptRoleVo();
            cloudDeptRoleVo.setId(String.valueOf(myDeptRoleEntity.get("id")));
            cloudDeptRoleVo.setDeptId(String.valueOf(myDeptRoleEntity.get("dept_id")));
            cloudDeptRoleVo.setDeptRoleStatus(String.valueOf(myDeptRoleEntity.get("dept_role_status")));
            cloudDeptRoleVo.setRoleId(String.valueOf(myDeptRoleEntity.get("role_id")));
            cloudDeptRoleVo.setDescription(String.valueOf(myDeptRoleEntity.get("description")));
            cloudDeptRoleVo.setRoleName(String.valueOf(myDeptRoleEntity.get("name")));

            vos.add(cloudDeptRoleVo);
        }

        return vos;

    }

    public List<TreeVo> coverTreeVoList(List<MyDeptPostsEntity> myDeptPostsEntitys){
        Iterator<MyDeptPostsEntity> ite = myDeptPostsEntitys.iterator();
        List<TreeVo> treeVos = new ArrayList<>();
        while (ite.hasNext()){
            MyDeptPostsEntity cloudDeptPostsEntity = ite.next();
            TreeVo treeVo = new TreeVo();
            treeVo.setId(String.valueOf(cloudDeptPostsEntity.getId()));
            treeVo.setParentId(String.valueOf(cloudDeptPostsEntity.getPid()));
            treeVo.setName(cloudDeptPostsEntity.getName());
            treeVo.setValue(cloudDeptPostsEntity.getType());
            treeVo.setStatus(cloudDeptPostsEntity.getDeptPostsStatus());
            treeVos.add(treeVo);
        }

        return treeVos;

    }

    /**
     * list转vo
     * @author zyj
     * @date 2019/9/24 11:14
     * @param myDeptPostsEntities
     * @return
     */
    public List<MyDeptPostsVo> coverVoList(List<MyDeptPostsEntity> myDeptPostsEntities){
        Iterator<MyDeptPostsEntity> iterator = myDeptPostsEntities.iterator();
        List<MyDeptPostsVo> cloudDeptPostsVos = new ArrayList<>();
        while (iterator.hasNext()){
            MyDeptPostsEntity cloudDeptPostsEntity = iterator.next();
            MyDeptPostsVo cloudDeptPostsVo = new MyDeptPostsVo();
            cloudDeptPostsVo.setId(String.valueOf(cloudDeptPostsEntity.getId()));
            cloudDeptPostsVo.setName(cloudDeptPostsEntity.getName());
            cloudDeptPostsVo.setDeptPostsStatus(cloudDeptPostsEntity.getDeptPostsStatus());
            cloudDeptPostsVo.setPid(String.valueOf(cloudDeptPostsEntity.getPid()));
            cloudDeptPostsVo.setType(cloudDeptPostsEntity.getType());

            cloudDeptPostsVos.add(cloudDeptPostsVo);
        }

        return cloudDeptPostsVos;
    }

    /**
     * 数据
     * @return
     */
    public List<TreeVo> getChildrenTree(String id){

//        List<MyAuthEntity> resourceList = myAuthDao.getAuthByParentId(null);
//        listTrees = treeVoUtil.convertTreeVo(CommonUtils.coverTree(resourceList), "1");
        List<TreeVo> listTrees = null;
        List<MyDeptPostsEntity> resourceList = null ;
        List<MyDeptPostsEntity> childrenRe = new ArrayList<>();
        try {

            listTrees = new ArrayList<>();
            resourceList = myDeptPostsDao.getDeptByParentId(null);
            Iterator<MyDeptPostsEntity> iterator = resourceList.iterator();
            while (iterator.hasNext()){//查找id对应子节点
                MyDeptPostsEntity resource = iterator.next();
                Long parentId = resource.getPid();
                long localId = resource.getId();
                if(parentId!=null&&parentId.equals(Long.parseLong(id))&&localId!=parentId){
                    childrenRe.add(resource);
                }
            }

            //转TreeVo
            listTrees = coverTree(childrenRe, resourceList);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return  listTrees;
    }

    /**
     * 转换成树结构
     * @param resourceList 需要转换的list
     * @param resourceList1 所有资源
     * @return
     */
    public List<TreeVo> coverTree(List<MyDeptPostsEntity> resourceList, List<MyDeptPostsEntity> resourceList1){
        List<TreeVo> listTree = new ArrayList<>();
        if(resourceList!=null&&resourceList.size()>0){
            Iterator<MyDeptPostsEntity> iterator = resourceList.iterator();
            while (iterator.hasNext()){
                MyDeptPostsEntity next = iterator.next();
                long id = next.getId();
                TreeVo treeVo = new TreeVo();
                treeVo.setId(String.valueOf(id));
                treeVo.setName(next.getName()+"-"+id);
                treeVo.setParentId(String.valueOf(next.getPid()));
                //判断是否为父级
                boolean b = false;
                Iterator<MyDeptPostsEntity> iterator1 = resourceList1.iterator();
                while (iterator1.hasNext()){
                    MyDeptPostsEntity next1 = iterator1.next();
                    if (next1.getPid()==id){
                        b = true;
                    }
                }

                treeVo.setIsParent(b);
                listTree.add(treeVo);
            }
        }

        return  listTree;
    }

    /**
    * @Description: 根据部门
    * @Param: [deptId]
    * @return: java.util.List<com.common.vo.TreeVo>
    * @Author: zyj
    * @Date: 2020/1/13
    */
    public List<TreeVo> getDeptRoleTreeList(Long deptId){
        //获取所有角色列表
        List<MyRoleEntity> roleEntities = myRoleDao.selectList();
        List<Map<String, Object>> list = myDeptRoleDao.findRoleByDeptId(deptId);
        if(null==roleEntities||roleEntities.isEmpty()){
            return null;
        }

        return coverDeptRoleTree(roleEntities, list);

    }

    /**
    * @Description: 部门角色列表转化为树节点
    * @Param: [roleEntities, list]
    * @return: java.util.List<com.common.vo.TreeVo>
    * @Author: zyj
    * @Date: 2020/1/13
    */
    public List<TreeVo> coverDeptRoleTree(List<MyRoleEntity> roleEntities, List<Map<String, Object>> deptRoleList){
        List<TreeVo> treeVoList = new ArrayList<>();
        for(int i=0; i<roleEntities.size(); i++){
            MyRoleEntity myRoleEntity = roleEntities.get(i);
            TreeVo treeVo = new TreeVo();
            String id = String.valueOf(myRoleEntity.getId());
            treeVo.setId(id);
            treeVo.setName(myRoleEntity.getName());
            treeVo.setValue(myRoleEntity.getRoleCode());
            for(int j=0; j<deptRoleList.size(); j++){
                Map<String, Object> myDeptRoleVo = deptRoleList.get(j);
                String roleId = String.valueOf(myDeptRoleVo.get("role_id"));
                if(id.equals(roleId)){
                    treeVo.setChecked(true);
                }
            }

            treeVoList.add(treeVo);
        }

        return treeVoList;
    }


    /**
     * 根据部门id获取
     * @author zyj
     * @date 2019/9/24 20:07
     * @param deptId
     * @return
     */
    public List<TreeVo> getUserTreeList(Long deptId){
        List<MyUserEntity> userEntityList = myUserDao.listUser();

        List<MyDeptUserEntity> myDeptUserEntities = myDeptUserDao.findByDeptId(deptId);
        if(null==userEntityList||userEntityList.isEmpty()){
            return null;
        }

        return coverDeptUserList(userEntityList, myDeptUserEntities);
    }

    /**
    * @Description: 部门用户树列表
    * @Param: [userEntityList, myDeptUserEntities]
    * @return: java.util.List<com.common.vo.TreeVo>
    * @Author: zyj
    * @Date: 2020/1/13
    */
    public List<TreeVo> coverDeptUserList(List<MyUserEntity> userEntityList,List<MyDeptUserEntity> myDeptUserEntities){
        List<TreeVo> treeVoList = new ArrayList<>();
        for (int i=0; i<userEntityList.size(); i++){
            MyUserEntity myUserEntity = userEntityList.get(i);
            TreeVo treeVo = new TreeVo();
            int id = myUserEntity.getId();
            treeVo.setId(String.valueOf(id));
            treeVo.setName(myUserEntity.getUserName());
            for(int j=0; j<myDeptUserEntities.size(); j++){
                MyDeptUserEntity myDeptUserEntity = myDeptUserEntities.get(j);
                long usrId = myDeptUserEntity.getUserId();
                if(id==usrId){
                    treeVo.setChecked(true);
                }
            }

            treeVoList.add(treeVo);
        }

        return treeVoList;
    }

    @Transactional(value="transactionManager", rollbackFor = Exception.class)
    public void updateDeptRole(List<MyDeptRoleDto> myDeptRoleDtos){
        if(null==myDeptRoleDtos||myDeptRoleDtos.isEmpty()){
            return;
        }

        Long deptId = Long.parseLong(myDeptRoleDtos.get(0).getDeptId());
        myDeptRoleDao.deleteByDeptId(deptId);
        myDeptRoleDao.insertDeptRoleList(myDeptRoleDtos);
    }

    @Transactional(value="transactionManager", rollbackFor = Exception.class)
    public void updateDeptUser(List<MyDeptUserDto> myDeptUserDtos){
        if(null==myDeptUserDtos||myDeptUserDtos.isEmpty()){
            return;
        }

        Long deptId = Long.parseLong(myDeptUserDtos.get(0).getDeptId());
        myDeptUserDao.deleteDeptUserByDeptId(deptId);
        myDeptUserDao.insertDeptUserList(myDeptUserDtos);
    }
}
